package com.haiheng.module_main.dialog

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.ArrayAdapter
import com.haiheng.common.activity.RouterActivity
import com.haiheng.common.constant.RourePage
import com.haiheng.library_base.dialog.FullDialog
import com.haiheng.module_main.R
import com.haiheng.module_main.adapter.CustomSpinnerAdapter
import com.haiheng.module_main.databinding.CancelDeliveryOrderDialogBinding


/**
 *
 * @Des:
 * @Author: hwz
 * @Date:2024/8/23 0023
 * @Version: 1.0
 */
class CancelDeliveryOrderDialog  : FullDialog() {
    private var listener: OnDialogClickListener? = null
    private var selectReadon:String = ""

    companion object{
        @JvmStatic
        fun newInstance(): CancelDeliveryOrderDialog{
            return CancelDeliveryOrderDialog()
        }
    }

    private lateinit var binding:CancelDeliveryOrderDialogBinding


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        //设置背景透明，才能显示出layout中诸如圆角的布局，否则会有白色底（框）
        dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        dialog?.window?.decorView?.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                or View.SYSTEM_UI_FLAG_FULLSCREEN
                or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
        binding = CancelDeliveryOrderDialogBinding.inflate(inflater,container,false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        dialog?.window?.apply { setDimAmount(0.5f) }
        binding?.ivClose.setOnClickListener {
            dismiss()
        }
        iniDate()
    }

    fun iniDate(){

        binding.slRight.setOnClickListener {
            (activity as RouterActivity).navigation(RourePage.BOOK_CAR_SCAN)
        }

        binding.slLeft.setOnClickListener {
            listener?.onLeftClick(this)
        }

        binding.ivClose.setOnClickListener {
            dismiss()
        }

        binding.slRight.setOnClickListener {
            listener?.onRightClick(binding.etResean.text.toString(),selectReadon,this)
        }
        var list = resources.getStringArray(R.array.arr_quit_reason)
        var listValue = resources.getStringArray(R.array.arr_quit_value)
        selectReadon = list.get(0)
        binding.etResean.setText(listValue.get(0))
        val adapter = CustomSpinnerAdapter(requireContext(), R.layout.spinner_item, // 注意：这个布局可能不会被直接使用，取决于你的实现
            R.id.text_view_in_spinner_item, list)

        binding.spinnerLocation.adapter = adapter
        binding.spinnerLocation.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
               selectReadon = adapter.getItem(position).toString()
                binding.etResean.setText(listValue.get(position))
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
                // 可选实现
            }
        }



    }
    fun setOnDialogListener(listener: OnDialogClickListener):CancelDeliveryOrderDialog{
        this.listener = listener
        return this
    }
    interface OnDialogClickListener {

        fun onLeftClick(dialog: CancelDeliveryOrderDialog)

        fun onRightClick(resean:String,selectReson:String,dialog: CancelDeliveryOrderDialog)

    }

}